package org.huang.sample10.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.huang.sample10.config.support.MybatisConfigurationSupport;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;

/**
 * @描述：DB1Config类描述
 * @作者: huanganwei
 * @创建时间: 2021-07-31 00:43:36
 * @版本: 1.0
 */
@Configuration
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = "org.huang.sample10.dal.dao.db3",
        sqlSessionFactoryRef = "db3SqlSessionFactory",
        sqlSessionTemplateRef = "db3SqlSessionTemplate")
public class DB3Config {


    @Bean("db3MybatisProperties")
    @ConfigurationProperties(prefix = "mybatis.db3") //读取application.yml中的配置参数映射成为一个对象
    public MybatisProperties db3MybatisProperties(){
        return new MybatisProperties();
    }


    @Bean("db3DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db3") //读取application.yml中的配置参数映射成为一个对象
    public DataSource db3DataSource(){
        return new DruidDataSource();
    }

    @Bean("db3SqlSessionFactory")
    public SqlSessionFactory db3SqlSessionFactory() throws Exception {
        return MybatisConfigurationSupport.sqlSessionFactoryOf(db3MybatisProperties(),db3DataSource());
    }

    @Bean("db3SqlSessionTemplate")
    public SqlSessionTemplate db3SqlSessionTemplate() throws Exception {
        return new SqlSessionTemplate(db3SqlSessionFactory());
    }


    @Bean("db3TransactionManager")
    public PlatformTransactionManager db3TransactionManager() {
        return new DataSourceTransactionManager(db3DataSource());
    }

    @Bean("db3TransactionTemplate")
    public TransactionTemplate db3TransactionTemplate(){
        return new TransactionTemplate(db3TransactionManager());
    }



    /*************************************  druid  监控 servlet *************************************/
    @Bean
    public ServletRegistrationBean druidServlet(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        bean.addInitParameter("allow","127.0.0.1");
        bean.addInitParameter("resetEnable","false");
        bean.addInitParameter("loginUsername","admin");
        bean.addInitParameter("loginPassword","admin");
        return bean;
    }

    @Bean
    public FilterRegistrationBean statFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter());
        bean.addUrlPatterns("/*");
        bean.addInitParameter("exclusions","*.js,*.gif,/druid/*");
        return bean;
    }
    /*************************************  druid  监控 servlet *************************************/
}
